<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin: 10px 0px"
      @click="showDialog"
    >
      添加
    </el-button>

    <!-- 表格组件 
    data: 表格组件将来展示的数据
    border: 给表格添加边框
    prop: 列的属性名
    label: 列的标题
    -->
    <el-table :data="list" style="width: 100%" border>
      <el-table-column
        type="index"
        prop="prop"
        label="序号"
        width="80px"
        align="center"
      />
      <el-table-column prop="tmName" label="品牌名称" width="width" />
      <el-table-column prop="prop" label="品牌LOGO" width="width">
        <!-- row, $index  -->
        <template slot-scope="{ row }">
          <img :src="row.logoUrl" alt="" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateTradeMark(row)"
          >
            修改
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[3, 5, 10]"
      :pager-count="7"
      style="margin-top: 20px; text-align: center"
      layout="prev, pager, next, jumper,->, sizes, total"
      @current-change="getPageList"
      @size-change="handleSizeChange"
    />

    <!-- 对话框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <!-- 展示表单元素 -->
      <el-form style="width: 80%" :model="tmForm" :rules="rules">
        <el-form-item label="品牌名称" label-width="100px" prop="tmName">
          <el-input autocomplete="off" v-model="tmForm.tmName" />
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
          <!-- 图片上传 -->
          <!-- 这里收集数据不能用v-model 不是表单元素-->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取 消 </el-button>
        <el-button type="primary" @click="addOrUpdateTradeMark">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "TardeMark",
  data() {
    return {
      // 显示分页器第几页
      page: 1,
      // 当前页数展示的数据条数
      limit: 3,
      // 总共的数据条数
      total: 0,
      // 列表展示的数据
      list: [],
      //对话框显示与隐藏的控制
      dialogFormVisible: false,
      // 收集品牌的信息
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
      //表单验证规则
      rules: {
        // 品牌名称的验证规则
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "change",
          },
        ],
        logoUrl: [
          { required: true, message: "请选择品牌图片", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    // 计算属性
    // 当前页展示的数据
    data() {
      return this.list.slice(
        (this.page - 1) * this.limit,
        this.page * this.limit
      );
    },
  },
  mounted() {
    // 获取列表数据的方法
    this.getPageList();
  },
  methods: {
    // 获取品牌列表的数据
    async getPageList(pager = 1) {
      this.page = pager;
      console.log("点击后", this.page);

      // 解构出参数
      const { page, limit } = this;
      let result = await this.$API.tradeMark.reqTradeMarkList(page, limit);
      // console.log("🚀 ~ getPageList ~ result:", result);
      if (result.code === 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
    },

    //当分页器的展示数据改变时会触发
    handleSizeChange(limit) {
      // 整理参数再发请求
      this.limit = limit;
      this.getPageList();
    },
    // 点击添加品牌按钮
    showDialog() {
      //显示对话框
      this.dialogFormVisible = true;
      //清除数据
      this.tmForm = {
        tmName: "",
        logoUrl: "",
      };
    },
    //修改某一个品牌
    updateTradeMark(row) {
      //显示对话框
      this.dialogFormVisible = true;
      // 将已有的品牌信息进行回显
      // console.log(row);

      this.tmForm = { ...row };
    },
    // 图片上传成功
    handleAvatarSuccess(res, file) {
      // file是上传成功后服务器返回前端的数据
      // console.log("🚀 ~ handleAvatarSuccess ~ file:", file);
      // res是上传成功后返回给前端的数据
      // console.log("🚀 ~ handleAvatarSuccess ~ res:", res);
      // 收集品牌图片的数据,将来带给服务器
      this.tmForm.logoUrl = res.data;
    },

    // 图片上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 添加按钮(添加 修改品牌)
    async addOrUpdateTradeMark() {
      this.dialogFormVisible = false;
      //发请求
      let result = await this.$API.tradeMark.reqAddOrUpdateTradeMark(
        this.tmForm
      );
      // console.log("🚀 ~ 品牌添加修改:", result);
      if (result.code == 200) {
        // 弹出信息
        this.$message({
          type: "success",
          message: this.tmForm.id ? "修改品牌成功" : "添加品牌成功",
        });
        // 重新获取列表数据
        this.getPageList(this.tmForm.id ? this.page : 1);
      }
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
